import os
from selenium import webdriver
import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

# 打开浏览驱动器
driver = webdriver.Chrome()
driver.implicitly_wait(3)

# 将浏览器最大化
driver.maximize_window()

# 获取当前目录的selen.html文件
file_path = os.path.join('file:///', os.getcwd(), "selenium.html")
driver.get(file_path)

# 定位元素并操作元素
username_element = driver.find_element_by_css_selector('div#signup input[name="username"]')
password_element = driver.find_element_by_css_selector('div#signup input[name="password"]')
username_element.send_keys("rabbit")
password_element.send_keys("love")

time.sleep(1)
# 下拉选择城市 深圳
form = Select(driver.find_element_by_name('city'))
form.select_by_value('sz')

time.sleep(1)
# 点击提交
form = driver.find_element_by_css_selector('div#signup form')
form.submit()

# 显示等待
res = WebDriverWait(driver, 11, 1).until(lambda d: d.find_element_by_css_selector("div#signup p").text == "注册成功")
print('检查是否通过:', res)

# 关闭浏览器
driver.quit()
